package com.pet.service.impl;


import com.pet.context.BaseContext;
import com.pet.dto.web.PetInformationDTO;
import com.pet.entity.PetAdoptInfo;
import com.pet.entity.PetInformation;
import com.pet.exchange.PetInfoUtil;
import com.pet.mapper.*;
import com.pet.service.PetInformationService;
import com.pet.vo.web.PetInformationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class PetInformationServiceImpl implements PetInformationService {

    @Autowired
    private PetInformationMapper petInformationMapper;
    @Autowired
    private UserInformationMapper userInformationMapper;
    @Autowired
    private PetInfoQuestionnaireMapper petInfoQuestionnaireMapper;
    @Autowired
    private PetAdoptApplicationMapper petAdoptApplicationMapper;
    @Autowired
    private PetAdoptMapper petAdoptMapper;
    @Autowired
    private PetInfoUtil petInfoUtil;

    /**
     * 添加宠物信息
     * @param petInformationDTO
     */
    public void insertPetInformation(PetInformationDTO petInformationDTO) {
        //创建实体
        PetInformation petInformation = new PetInformation();
        //赋值
        BeanUtils.copyProperties(petInformationDTO, petInformation);
        petInformation.setCreateUser(Math.toIntExact(BaseContext.getCurrentId()));
        petInformation.setCreateTime(LocalDateTime.now());
        petInformation.setUpdateUser(Math.toIntExact(BaseContext.getCurrentId()));
        petInformation.setUpdateTime(LocalDateTime.now());
        log.info("petInformation:{}",petInformation);
        //操作数据库
        petInformationMapper.insertPetInformation(petInformation);
    }

    /**
     * 查询所有宠物信息
     * @return
     */
    public List<PetInformationVO> selectPetInformationAll() {
        //创建实体数组 获取实体数组
        List<PetInformation> petInformationList = petInformationMapper.selectPetInformationAll();
        //创建VOList
        List<PetInformationVO> petInformationVOList = new ArrayList<>();
        //赋值
        for (PetInformation petInformation : petInformationList) {
            PetInformationVO petInformationVO = new PetInformationVO();
            BeanUtils.copyProperties(petInformation, petInformationVO);

            //对 接种疫苗情况 是否驱虫情况 是否绝育情况进行判断
            if ( petInformation.getVaccine() == 1){
                petInformationVO.setVaccine("已接种疫苗");
            }else{
                petInformationVO.setVaccine("未接种疫苗");
            }

            if ( petInformation.getInsectRepellent() == 1){
                petInformationVO.setInsectRepellent("已驱虫");
            }else{
                petInformationVO.setVaccine("未驱虫");
            }

            if ( petInformation.getSterilization() == 1){
                petInformationVO.setSterilization("已绝育");
            }else{
                petInformationVO.setSterilization("未绝育");
            }
            petInformationVO.setCreateUser(userInformationMapper.selectName(petInformation.getCreateUser()));
            petInformationVOList.add(petInformationVO);
        }
        return petInformationVOList;
    }

    /**
     * 修改宠物信息
     * @param dto
     */
    public void updatePetInformation(PetInformationDTO dto) {
        log.info("修改宠物信息dto:{}",dto);
        PetInformation e = new PetInformation();
        BeanUtils.copyProperties(dto,e);
        //对字段进行判断修改
        e.setUpdateUser(Math.toIntExact(BaseContext.getCurrentId()));
        e.setUpdateTime(LocalDateTime.now());
        log.info("修改宠物信息e:{}",e);
        petInformationMapper.updatePetInformation(e);
    }

    /**
     * 删除宠物信息
     * @param id
     */
    @Transactional
    public void deletePetInformation(Integer id) {
        //删除pet_information_questionnaire
        petInfoQuestionnaireMapper.deleteQuestionnaireByPetId(id);
        //删除pet_adopt_application
        petAdoptApplicationMapper.deletePetAdoptApplicationByPetId(id);
        //删除pet_adopt_info
        petAdoptMapper.deletePetAdoptInfoByPetId(id);
        //删除本身
        petInformationMapper.deletePetInformation(id);
    }

    /**
     * 查询个人宠物信息
     * @return
     */
    public List<PetInformationVO> selectPetSelfInformation() {
        //创建实体数组 获取实体数组
        List<PetInformation> petInformationList = petInformationMapper.selectPetSelfInformation(BaseContext.getCurrentId());
        //创建VOList
        List<PetInformationVO> petInformationVOList = new ArrayList<>();
        //赋值
        for (PetInformation petInformation : petInformationList) {
            //对 接种疫苗情况 是否驱虫情况 是否绝育情况进行判断
            PetInformationVO petInformationVO = petInfoUtil.exchangePetInfoUtil(petInformation);

            petInformationVO.setCreateUser(userInformationMapper.selectName(petInformation.getCreateUser()));
            petInformationVOList.add(petInformationVO);
        }
        return petInformationVOList;
    }

    /**
     * 根据用户id查询宠物信息
     * @param userId
     * @return
     */
    public List<PetInformationVO> selectPetInformationPersonal(Integer userId) {
        //创建实体数组 获取实体数组
        List<PetInformation> petInformationList = petInformationMapper.selectPetSelfInformation(Long.valueOf(userId));
        //创建VOList
        List<PetInformationVO> petInformationVOList = new ArrayList<>();
        //赋值
        for (PetInformation petInformation : petInformationList) {
            //对 接种疫苗情况 是否驱虫情况 是否绝育情况进行判断
            PetInformationVO petInformationVO = petInfoUtil.exchangePetInfoUtil(petInformation);

            petInformationVO.setCreateUser(userInformationMapper.selectName(petInformation.getCreateUser()));
            petInformationVOList.add(petInformationVO);
        }
        return petInformationVOList;
    }
}
